//index.js
//获取应用实例
import { api } from '../../utils/config';
var template = require('../../static/template/template.js')
const app = getApp();
let page = 1;//初始页面
Page({
  data: {
    wuhan: '武汉',
    imgUrls: [],
    productList:[],
    indicatorDots: true,
    autoplay: true,
    interval: 5000,
    duration: 500,
    // circular: true,
    indicatorActiveColor: "#da1a1e",//当前选中的指示点颜色
    indicatorColor: "rgba(255, 255, 255, .9)",//指示点颜色
    total:'',//总页数
    cityAreaId:'',//城市地址Id,
    newList: ['aaa', 'bbb','ccc']
  },
  //事件处理函数
  bindViewTap: function () {
    wx.navigateTo({
      url: '../logs/logs'
    })
  },
  //拼团介绍页面跳转
  checkIntroduce:function(){
    wx.navigateTo({
      url: '../introduce/introduce'
    })
  },
  onLoad: function () {
    let that = this;
    template.tabbar("tabBar", 0, this)//0表示第一个tabbar
    // 后期可删掉
    // let url = api + "/api/fg/cityList";
    // app.httpClient.post(url).then(res => {
    //   let cityAreaId = res.data.obj.cityKtList[0].cityAreaId;
    //   that.setData({
    //     wuhan: res.data.obj.cityKtList[0].cityName
    //   })
    //   wx.setStorage({
    //     key: "cityAreaId",
    //     data: cityAreaId
    //   })
    // }),
    //获取城市缓存
    wx.setNavigationBarTitle({
      title: '大件会',
    })
    app.getUserInfo()
    wx.getStorage({
      key: 'cityInfo',
      success: function (res) {
        that.setData({ wuhan: res.data.cityName })
        // let datas = {
        //   areaId: res.data.areaId,
        //   sord: 'desc',
        //   page: page,
        //   rows: 15,
        // }
        // console.log(datas)
        // let url = api + "/api/fg/index";
        // app.httpClient.post(url, datas).then(res => {
        //   console.log(res)
        //   let bannerList = res.data.obj.bannerList;
        //   let fgList = res.data.obj.fgList.rows;
        //   let status = res.data.obj.fgList.rows.status;
        //   // console.log(fgList);
        //   that.setData({
        //     imgUrls: bannerList,
        //     productList: fgList
        //   })
        // })
      }
    })
    // this.onToLoadDatas(this.data.cityAreaId, page)
    var url = api + "/api/index"
    var data = {
      areaId: this.data.cityAreaId,
      source: 13
    }
    wx.showLoading({
      title: '加载中',
      mask: true
    })
    app.httpClient.post(url, data).then((res) => {
      console.log(res)
      if(res.data.code==200) {
        wx.hideLoading()
        that.setData({
          imgUrls: res.data.obj.bannerList,
          newList: res.data.obj.saidList,
          productList: res.data.obj.gpList
        })
      }
    }) 
  },
  // 上拉加载
  // onReachBottom: function () {
  //   let that = this;
  //   console.log('上拉');
  //   if (page<=this.data.total-1){
  //     page++;
  //     console.log(page)
  //     this.onGetpages(this.data.cityAreaId,page)
  //   }
  // },
  // 下拉刷新
  onPullDownRefresh:function(){
    wx.showNavigationBarLoading();//在标题栏中显示加载
    let that = this;
    // setTimeout(function () {
      // this.onToLoadDatas(this.data.cityAreaId, page)
      // wx.hideNavigationBarLoading() //完成停止加载
      // wx.stopPullDownRefresh() //停止下拉刷新
    // }, 500);
    //  this.onToLoadDatas(this.data.cityAreaId, page)
    var url = api +"/api/index"
    api.httpClient.post(url,data).then((res) =>{
      // if(){}
      wx.hideNavigationBarLoading()
      wx.stopPullDownRefresh()
    })
  },
 //封装上拉加载请求
  onGetpages: function (cityAreaId,page){
    let url = api + "/api/index";
   let datas = {
     areaId: cityAreaId,
     sord: 'desc',
     page: page,
     rows: 15,
   }
   app.httpClient.post(url, datas).then(res => {
     let bannerList = res.data.obj.bannerList;
     let fgList = res.data.obj.fgList.rows;
     let total = res.data.obj.fgList.page.total;//总页面
     console.log(res)
     this.setData({
       productList: this.data.productList.concat(fgList),
       total: total
     })
     console.log(this.data.productList)
   })
 },
  //封装首页页面第一屏的数据加载请求
  onToLoadDatas: function (cityAreaId, page) {
    let url = api + "/api/index";
    let datas = {
      areaId: cityAreaId,
      sord: 'desc',
      page: page,
      rows: 15,
      source: 13
    }
    return app.httpClient.post(url,datas).then(res => {
      let bannerList = res.data.obj.bannerList;
      let fgList = res.data.obj.fgList.rows;
      console.log(fgList)
      let total = res.data.obj.fgList.page.total
      this.setData({
        imgUrls: bannerList,
        productList: fgList,
        total: total
      })
    })
  },
  //扫码
  saoCode: function() {
    wx.scanCode({
      success: (res) => {
        console.log(res)
        wx.navigateTo({
          url: './earnest/earnest',
          data: {}
        })
      },
      fail: (res) => {
        console.log(res)
      }
    })
  }
  //封装首页请求
})

